﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Negocio.Marca;

namespace qbaproject.Marca.Consultar
{
    public partial class consultarMarca : System.Web.UI.Page
    {
        private int paginaAtual = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //carregarDdl();
            }
        }


        protected void lkbPesquisar_Click(object sender, EventArgs e)
        {
            carregarGrid();
        }

        public void carregarGrid()
        {
            
            var obNMarca = new NMarca();
            var obListMarca = obNMarca.pegarMarcas(txtNmMarca.Text, txtDsMarca.Text);

            if (obListMarca.Tables[0].Rows.Count != 0)
            {
                divMsnPesquisaSucesso.Visible = true;
                lblNumEncontrados.Text = "Foram encontrados " + obListMarca.Tables[0].Rows.Count.ToString() + " registros.";
                divMsnPesquisaAtencao.Visible = false;
            }
            else
            {
                divMsnPesquisaSucesso.Visible = false;
                divMsnPesquisaAtencao.Visible = true;
            }

            grdResMarca.PageSize = 15;
            grdResMarca.AllowPaging = true;
            grdResMarca.PageIndex = paginaAtual;
            grdResMarca.DataSource = obListMarca;
            grdResMarca.DataBind();
        }

        protected void grdResMarca_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            paginaAtual = e.NewPageIndex;
            carregarGrid();
        }

        protected void lkbSim_Click(object sender, EventArgs e)
        {
            var idMarca = Convert.ToInt32(hdfIdMarcaRemover.Value);
            var obNMarca = new NMarca();
            obNMarca.Remover(idMarca);
            carregarGrid();
        }

        protected void lkbNao_Click(object sender, EventArgs e)
        {
            ModalPopupRemover.Hide();
        }

        protected void grdResMarca_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "editar":
                    var idMarca = e.CommandArgument.ToString();
                    Response.Redirect("~/Marca/Editar/editarMarca.aspx?id=" + idMarca);
                    break;
                case "remover":
                    var idMarcaRemover = e.CommandArgument.ToString();
                    hdfIdMarcaRemover.Value = idMarcaRemover;
                    ModalPopupRemover.Show();
                    break;
            }

        }

        protected void lnk_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Marca/Incluir/incluirMarca.aspx");
        }

    }

}